package com.rojored.controls.plotClasses
{

import flash.display.Shader;
import flash.utils.ByteArray;

/**
 *  Provides basic functionality and API for shaders used by the Plot
 *  component.
 */
public class PlotShaderBase extends Shader
{

    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     *  Constructor
     */
    public function PlotShaderBase(byteCode:ByteArray)
    {
        super(byteCode);
    }

    //--------------------------------------------------------------------------
    //
    //  Variables
    //
    //--------------------------------------------------------------------------

    /**
     *  @private
     */
    protected var lineRGBA:Array = [0.0, 0.0, 0.0, 0.0];

    /**
     *  @private
     */
    protected var backgroundRGBA:Array = [0.0, 0.0, 0.0, 0.0];

    //--------------------------------------------------------------------------
    //
    //  Properties
    //
    //--------------------------------------------------------------------------

    //--------------------------------------
    //  lineColor
    //--------------------------------------

    /**
     *  @private
     *  Storage for the lineColor property
     */
    private var _lineColor:uint;

    /**
     *  Line color  (24-bit)
     */
    public function get lineColor():uint
    {
        return _lineColor;
    }

    /**
     *  @private
     */
    public function set lineColor(value:uint):void
    {
        trace("set lineColor", value.toString(16));
        if (_lineColor == value)
            return;

        _lineColor = value;
        lineRGBA[0] = (value >> 16) / 256;

        trace((value >> 16).toString(16));

        lineRGBA[1] = (value >> 8 & 0xFF) / 256;

        trace((value >> 8 & 0xFF).toString(16));

        lineRGBA[2] = (value & 0xFF) / 256;
        trace((value & 0xFF).toString(16));

        data.lineColor.value = lineRGBA;
    }

    //--------------------------------------
    //  lineAlpha
    //--------------------------------------

    /**
     *  @private
     *  Storage for the lineAlpha property
     */
    private var _lineAlpha:Number;

    /**
     *  Line alpha (0.0 to 1.0)
     */
    public function get lineAlpha():Number
    {
        return _lineAlpha;
    }

    /**
     *  @private
     */
    public function set lineAlpha(value:Number):void
    {
        if (_lineAlpha == value)
            return;

        _lineAlpha = value;
        lineRGBA[3] = value;
        data.lineColor.value = lineRGBA;
    }

    //--------------------------------------
    //  lineThickness
    //--------------------------------------

    /**
     *  @private
     *  Storage for the lineThickness property
     */
    private var _lineThickness:Number;

    /**
     *  Line thickness in pixels.
     */
    public function get lineThickness():Number
    {
        return _lineThickness;
    }

    /**
     *  @private
     */
    public function set lineThickness(value:Number):void
    {
        if (_lineThickness == value)
            return;

        value = Math.max(1, value);

        _lineThickness = value;
        data.lineThickness.value = [value];
    }

    //--------------------------------------
    //  backgroundColor 
    //--------------------------------------
    
    /**
     *  @private
     *  Storage for the backgroundColor property
     */
    private var _backgroundColor:uint;

    /**
     *  Color (24-bits) for the component's background.
     */
    public function get backgroundColor():uint
    {
        return _backgroundColor;
    }

    /**
     *  @private
     */
    public function set backgroundColor(value:uint):void
    {
        if (_backgroundColor == value)
            return;

        _backgroundColor = value;
        backgroundRGBA[0] = (value >> 16) / 256;
        backgroundRGBA[1] = (value >> 8 & 0xFF) / 256;
        backgroundRGBA[2] = (value & 0xFF) / 256;
        data.backgroundColor.value = backgroundRGBA;
    }
    
    //--------------------------------------
    //  backgroundAlpha 
    //--------------------------------------
    
    /**
     *  @private
     *  Storage for the backgroundAlpha property
     */
    private var _backgroundAlpha:Number;

    /**
     *  Alpha for the components background.
     */
    public function get backgroundAlpha():Number
    {
        return _backgroundAlpha;
    }

    /**
     *  @private
     */
    public function set backgroundAlpha(value:Number):void
    {
        if (_backgroundAlpha == value)
            return;

        _backgroundAlpha = value;
        backgroundRGBA[3] = value;
        data.backgroundColor.value = backgroundRGBA;
    }

    //--------------------------------------
    //  dataProvider
    //--------------------------------------

    /**
     *  @private
     *  Storage for the dataProvider property
     */
    private var _dataProvider:Vector.<Number>;

    /**
     *  @copy IPlotShader#dataProvider
     */
    public function get dataProvider():Vector.<Number>
    {
        return _dataProvider;
    }

    /**
     *  @private
     */
    public function set dataProvider(value:Vector.<Number>):void
    {
        if (_dataProvider == value)
            return;

        _dataProvider = value;

        value ||= new Vector.<Number>();

        data.dataProvider.input = value;
        data.dataProvider.width = value.length;
        data.dataProvider.height = 1;
        data.dataProviderLength.value = [value.length];

    }

    //--------------------------------------
    //  minimum
    //--------------------------------------

    /**
     *  @private
     *  Storage for the minimum property
     */
    private var _minimum:Number;

    /**
     *  Minimum number expected on the data provider.
     */
    public function get minimum():Number
    {
        return _minimum;
    }

    /**
     *  @private
     */
    public function set minimum(value:Number):void
    {
        if (_minimum == value)
            return;

        _minimum = value;
        data.minimum.value = [minimum];
    }

    //--------------------------------------
    //  maximum
    //--------------------------------------

    /**
     *  @private
     *  Storage for the maximum property
     */
    private var _maximum:Number;

    /**
     *  Maximum data expected on the data provider.
     */
    public function get maximum():Number
    {
        return _maximum;
    }

    /**
     *  @private
     */
    public function set maximum(value:Number):void
    {
        if (_maximum == value)
            return;

        _maximum = value;
        data.maximum.value = [maximum];
    }

    //--------------------------------------
    //  width
    //--------------------------------------

    /**
     *  @private
     *  Storage for the width property
     */
    private var _width:Number;

    /**
     *  Plot width.
     */
    public function get width():Number
    {
        return _width;
    }

    /**
     *  @private
     */
    public function set width(value:Number):void
    {
        if (_width == value)
            return;

        _width = value;
        data.width.value = [value];
    }

    //--------------------------------------
    //  height
    //--------------------------------------

    /**
     *  @private
     *  Storage for the height property
     */
    private var _height:Number;

    /**
     *  Plot height
     */
    public function get height():Number
    {
        return _height;
    }

    /**
     *  @private
     */
    public function set height(value:Number):void
    {
        if (_height == value)
            return;

        _height = value;
        data.height.value = [value];
    }
}
}
